#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(){
    int fd[2];
    pid_t pid;
    char buffer[20];
    //创建管道
    if(pipe(fd)<0){
        printf("Create pipe error!\n");
        exit(1);
    }
    //创建子进程
    pid = fork();
    if(pid < 0){
        printf("Create process error!\n");
        exit(1);
    }else if(pid == 0){
        //子进程读取管道中的数据
        close(fd[1]);
        int n = read(fd[0], buffer, sizeof(buffer));
        printf("Child process received: %s\n", buffer);
        close(fd[0]);
    }else{
        //父进程向管道中写入数据
        close(fd[0]);
        char* data = "Hello, child process!";
        write(fd[1], data, sizeof(data));
        printf("Parent process sent: %s\n", data);
        close(fd[1]);
    }
    return 0;
}